﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

namespace Scheduling.Models
{
    /// <summary>
    /// Model of a Booking containing necessary information
    /// </summary>
    public class Booking
    {
        /// <summary>
        /// Unique ID of a particular booking in form of an incremental integer.
        /// </summary>
        public int ID { get; set; }

        /// <summary>
        /// Course of the booking
        /// </summary>
        public CourseInstance CourseInstance{ get; set; }

        /// <summary>
        /// A booker; User that has booked the room.
        /// </summary>
        [Required]
        public User User { get; set; }

        /// <summary>
        /// Room that is being booked.
        /// </summary>
        [Required]
        public Room Room { get; set; }

        /// <summary>
        /// Time and date the booking starts
        /// </summary>
        [Required]
        public DateTime StartDate { get; set; }

        /// <summary>
        /// Time the booking ends.
        /// </summary>
        [Required]
        public DateTime EndDate { get; set; }

        /// <summary>
        /// Checks for collisions between bookings. 
        /// </summary>
        /// <param name="b">The booking to check for collisions. </param>
        /// <returns>True if collision, False otherwise. </returns>
        public bool CollidesWith(Booking b)
        {
            return (EndDate > b.StartDate && StartDate < b.EndDate && Room.ID == b.Room.ID);
        }

        public override string ToString()
        {
            if (CourseInstance == null)
                return "Invalid Booking";
            return string.Format("{0} ({1}) {2} {3}({4} {5} - {6})",
                Room.ID, User.ID, CourseInstance.Course.ID.PadRight(8), StartDate.ToShortDateString(), StartDate.DayOfWeek.ToString().PadRight(10), StartDate.ToShortTimeString(), EndDate.ToShortTimeString()
            );
        }
    }
}